package examtest;

import java.io.IOException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//多生产者消费者
public class homework9 {
    public static void main(String[] args) throws IOException {

        multiCondition();

    }


    static void multiCondition() throws IOException {
        //创建锁
        ReentrantLock lock=new ReentrantLock();
        final Condition notFull  = lock.newCondition();//写线程条件
        final Condition notEmpty = lock.newCondition();//读线程条件
        ItemsClass9 itemsClass=new ItemsClass9(0);

        Thread aa=new Thread(new MyThreadPlus9(lock,itemsClass,notFull,notEmpty),"读1");
        Thread bb=new Thread(new MyThreadMinus9(lock,itemsClass,notFull,notEmpty),"写1");
        Thread cc=new Thread(new MyThreadPlus9(lock,itemsClass,notFull,notEmpty),"读2");
        Thread dd=new Thread(new MyThreadMinus9(lock,itemsClass,notFull,notEmpty),"写2");
        aa.start();
        bb.start();
        cc.start();
        dd.start();
    }
}

//操作对象
class ItemsClass9{
    static Integer items;
    ItemsClass9(Integer items){
        this.items=items;
    }

}
//生产者
class MyThreadPlus9 implements Runnable{
    Lock lock;
    ItemsClass9 itemsClass;
    Condition notFull;
    Condition notEmpty;

    MyThreadPlus9(ReentrantLock lock,ItemsClass9 itemsClass,Condition notFull,Condition notEmpty){
        this.lock=lock;
        this.itemsClass=itemsClass;
        this.notFull=notFull;
        this.notEmpty=notEmpty;
    }

    @Override
    public void run() {
        while (itemsClass.items<= 100){
            lock.lock();
            try {
                while (itemsClass.items>= 100){
                    notFull.await();
                }
                itemsClass.items++;
                System.out.println(Thread.currentThread().getName()+"?正在写入，当前值为"+itemsClass.items);
                notEmpty.signal();
            }catch (Exception e){
                e.printStackTrace();
            }
            finally {
                lock.unlock();
            }
        }
    }
}
//消费者
class MyThreadMinus9 implements Runnable{
    Lock lock;
    ItemsClass9 itemsClass;
    Condition notFull;
    Condition notEmpty;

    MyThreadMinus9(ReentrantLock lock,ItemsClass9 itemsClass,Condition notFull,Condition notEmpty){
        this.lock=lock;
        this.itemsClass=itemsClass;
        this.notFull=notFull;
        this.notEmpty=notEmpty;
    }

    @Override
    public void run() {
        while (itemsClass.items>=0){
            lock.lock();
            try {
                while (itemsClass.items<=0){
                    notEmpty.await();
                }
                itemsClass.items--;
                System.out.println(Thread.currentThread().getName()+"?正在读出，当前值为"+itemsClass.items);
                notFull.signal();
            }catch (Exception e){
                e.printStackTrace();
            }
            finally {
                lock.unlock();
            }
        }
    }
}